package com.gpframework.module.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gpframework.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.module.module.domain.GpCommonBanner;
import com.gpframework.module.module.domain.vo.GpCommonBannerVo;
import com.gpframework.module.module.domain.req.GpCommonBannerReq;
import com.gpframework.module.module.domain.req.GpCommonBannerAdd;
import com.gpframework.module.module.domain.req.GpCommonBannerUpdate;
import com.gpframework.module.module.service.IGpCommonBannerService;
import com.gpframework.common.utils.poi.ExcelUtil;
import javax.validation.Valid;
import io.swagger.annotations.*;
import com.gpframework.common.core.page.TableDataInfo;
/**
 * @ClassName GpCommonBannerController
 * @Author gpframework
 * @Date 2021-12-09
 * @Version 1.0.0
 * @Description 首页推广图Controller
 */
@Validated
@Api(tags = "首页推广图")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/banner" )
public class GpCommonBannerController extends BaseController {

    private final IGpCommonBannerService iGpCommonBannerService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2021-12-09
  * @Param gpCommonBanner
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:banner:list')")
    @GetMapping("/list")
    public TableDataInfo<GpCommonBannerVo> list(GpCommonBannerReq gpCommonBannerReq)
    {
        startPage();
        QueryWrapper<GpCommonBanner> lqw = new QueryWrapper<GpCommonBanner>();
        lqw.orderByAsc("gp.sort");
        gpCommonBannerReq.generatorQuery(lqw,true);
        List<GpCommonBannerVo> list = iGpCommonBannerService.listVo(lqw);
        for (GpCommonBannerVo gpCommonBannerVo : list) {
            String identity = gpCommonBannerVo.getIdentity();
            if (StringUtils.isNotBlank(identity)) {
                String[] identityList = identity.split(",");
                gpCommonBannerVo.setIdentityList(identityList);
            }
        }
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonBanner
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:banner:add')" )
    @Log(title = "添加首页推广图" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpCommonBannerAdd gpCommonBannerAdd) {
        String[] identityList = gpCommonBannerAdd.getIdentityList();
        String identity = "";
        if (identityList != null && identityList.length > 0) {
            for (int i = 0; i < identityList.length; i++) {
                if (i == identityList.length - 1) {
                    identity += identityList[i];
                } else {
                    identity += identityList[i] + ",";
                }
            }
        }
        gpCommonBannerAdd.setIdentity(identity);
        GpCommonBanner gpCommonBanner = Convert.convert(new TypeReference<GpCommonBanner>() {}, gpCommonBannerAdd);
        return toAjax(iGpCommonBannerService.save(gpCommonBanner) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonBanner
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:banner:edit')" )
    @Log(title = "修改首页推广图" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpCommonBannerUpdate gpCommonBannerUpdate) {
        String[] identityList = gpCommonBannerUpdate.getIdentityList();
        String identity = "";
        if (identityList != null && identityList.length > 0) {
            for (int i = 0; i < identityList.length; i++) {
                if (i == identityList.length - 1) {
                    identity += identityList[i];
                } else {
                    identity += identityList[i] + ",";
                }
            }
        }
        gpCommonBannerUpdate.setIdentity(identity);
        GpCommonBanner gpCommonBanner = Convert.convert(new TypeReference<GpCommonBanner>() {}, gpCommonBannerUpdate);
        return toAjax(iGpCommonBannerService.updateById(gpCommonBanner) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonBanner
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:banner:export')" )
    @Log(title = "导出首页推广图" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpCommonBannerReq gpCommonBannerReq) {
        QueryWrapper<GpCommonBanner> lqw = new QueryWrapper<GpCommonBanner>();
        gpCommonBannerReq.generatorQuery(lqw,true);
        List<GpCommonBannerVo> list = iGpCommonBannerService.listVo(lqw);
        ExcelUtil<GpCommonBannerVo> util = new ExcelUtil<GpCommonBannerVo>(GpCommonBannerVo. class);
        return util.exportExcel(list, "banner" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonBanner
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:banner:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpCommonBannerVo> getInfo(@PathVariable("id" ) Long id) {
        GpCommonBannerVo gpCommonBannerVo = iGpCommonBannerService.getVoById(id);
        String identity = gpCommonBannerVo.getIdentity();
        if (StringUtils.isNotBlank(identity)) {
            String[] identityList = identity.split(",");
            gpCommonBannerVo.setIdentityList(identityList);
        }
        return AjaxResult.success(gpCommonBannerVo);
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2021-12-09
     * @Param gpCommonBanner
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:banner:remove')" )
    @Log(title = "删除首页推广图" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpCommonBannerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
